<%@page import="javax.xml.datatype.DatatypeFactory"%>
<%@page import="javax.xml.datatype.XMLGregorianCalendar"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="db.H2Adapter"%>
<%@page import="xml.jxb.Unit"%>
<%@page import="xml.jxb.SimpleMode"%>
<%@page import="xml.jxb.ProfilesProsent"%>
<%@page import="com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl"%>
<%@include file="WEB-INF/jspf/header.jspf"%>
<script type="text/javascript">
    <%@include file="scripts/create_proj_functions.js"%>

</script>
<%-- body here --%>
<% if (session.getAttribute("id") == null) {
        response.sendRedirect("login.jsp");
    }%>
<jsp:useBean id="Project" scope="session" class="xml.jxb.Project" />
<jsp:useBean id="OF" scope="session" class="xml.jxb.ObjectFactory" />
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="area">
    <fieldset class="fieldsetProj" id="idFldstCreateproject">
        <legend>Create project</legend>

        <fmt:message key="project.index.create" var="buttonValueCreate" />
        <input type="button" id="idButtonEnterCreate" value="${buttonValueCreate}"/>
        <br />
    </fieldset>
</div>
<%
    if (Project.getDescription() == null) {
        Project.setDescription(OF.createDescription());
        Project.getDescription().setProjectAuthor((String) session.getAttribute("name"));
        Project.getDescription().setProjectRevision(1);
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(new java.util.Date(System.currentTimeMillis()));
        XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        Project.getDescription().setProjectCreationDate(date2);
    }
    if (Project.getUnits() == null) {
        Project.setUnits(OF.createUnits());
    }
%>
<% if (request.getParameter("do") != null && request.getParameter("do").equals("create")) {%>
<%@include file="WEB-INF/jspf/project_create.jspf"%>
<%}%>

<% if (request.getParameter("do") != null && request.getParameter("do").equals("addLightingProfile")) {%>
<%@include file="WEB-INF/jspf/profile_lighting.jspf"%>
<%}%>

<% if (request.getParameter("do") != null && request.getParameter("do").equals("addEquipProfile")) {%>
<%@include file="WEB-INF/jspf/profile_equip.jspf"%>
<%}%>

<% if (request.getParameter("do") != null && request.getParameter("do").equals("addUnitSimple")) {%>
<%@include file="WEB-INF/jspf/create_unit_simple.jspf"%>
<%}%>

<% if (request.getParameter("do") != null && request.getParameter("do").equals("pickdate")) {%>
<%@include file="WEB-INF/jspf/datepicker.jspf"%>
<%}%>

<% if (request.getParameter("do") != null && request.getParameter("do").equals("addAdvancedUnit")) {%>
<%@include file="WEB-INF/jspf/create_unit_advanced.jspf"%>
<%}%>

<!--profile function -->
<% if (request.getParameter("mode") != null && request.getParameter("mode").equals("profile")) {
        if (request.getParameter("type").equals("lighting")) {
            String _name = request.getParameter("name");
            String _values = request.getParameter("values");
            if (Project.getProfiles() == null) {
                Project.setProfiles(OF.createProfiles());
            }
            ProfilesProsent pp = OF.createProfilesProsent();
            pp.setHourValues(_values);
            pp.setProfileName(_name);
            Project.getProfiles().getLightingProfile().add(pp);
        } else if (request.getParameter("type").equals("equip")) {
            String _name = request.getParameter("name");
            String _values = request.getParameter("values");
            if (Project.getProfiles() == null) {
                Project.setProfiles(OF.createProfiles());
            }
            ProfilesProsent pp = OF.createProfilesProsent();
            pp.setHourValues(_values);
            pp.setProfileName(_name);
            Project.getProfiles().getEqipmentProfile().add(pp);
        }
    }%>
<!--simple unit function -->
<% if (request.getParameter("mode") != null && request.getParameter("mode").equals("createSimple")) {
        String name = request.getParameter("unitName");
        int area = Integer.valueOf(request.getParameter("unitArea"));
        float volume = Float.valueOf(request.getParameter("unitVolume"));
        String uvalueId = request.getParameter("unitStandard");
        int kommuneId = Integer.valueOf(request.getParameter("unitKommuneId"));
        int lightingmaxEffect = Integer.valueOf(request.getParameter("unitMaxLight"));
        int equipTotalEffect = Integer.valueOf(request.getParameter("unitMaxEquip"));
        String ventType = request.getParameter("unitVentType");

        SimpleMode unit = OF.createSimpleMode();
        unit.setTotalArea(area);
        unit.setTotalVolume(volume);
        unit.setUvalueId(uvalueId);
        unit.setLocation(Integer.toString(kommuneId));
        unit.setLightingTotalEffect(lightingmaxEffect);
        unit.setEquipmentTotalEffect(equipTotalEffect);
        Unit _unit = OF.createUnit();
        _unit.setSimple(unit);
        Project.getUnits().getUnit().add(_unit);


    }%> 
<!--save project function -->        
<% if (request.getParameter("do") != null && request.getParameter("do").equals("saveProject")) {
        H2Adapter h2 = new H2Adapter();
            Project.getDescription().setProjectName("test");
        h2.saveProject((Integer) session.getAttribute("id"), Project);
        Project = null;
            h2.getCon().close();
    }%> 
<% if (request.getParameter("do") == null) {%>

<script src="scripts/projectlist.js"></script>


<fieldset>
    <legend>Projects</legend>
    <div class="projectList"></div>
</fieldset>    

<%}%>    
<%-- end of body --%>
<%@include file="WEB-INF/jspf/footer.jspf"%>